/*
 * FileName:       app_main_task.c
 * Author:         Kingsea  Version: v1.0.0  Date: 2020-3-4
 * Description:    
 * History:        
 *     <author>   	<version>    	<time>    	<desc>
 *     Kingsea   	v1.0.0    	2020-3-4    
 */
#include "main.h"
#include "app_main_task.h"
/****************************************************/

static TaskHandle_t m_main_thread = (TaskHandle_t)0; /**< Definition of main do thread. */

//////////////////////////////////////////////////////////////////////////
static void tk_main_writeSpeedCal()
{
	ret_code_t ret_val;
	uint16_t pkgLen = get_ble_nus_max_data_len();
	uint8_t* pszSend = pvPortMalloc(pkgLen);
	uint32_t nSumSend=0;

	for( uint16_t i=0; i< pkgLen; i++)
	{
		pszSend[i] = '\n';
	}
	if (pszSend)
	{
		TickType_t xLastTime = xTaskGetTickCount ();
	    do
	    {
	        ret_val = ble_nus_c_string_send(get_nus_c_object(), pszSend, pkgLen);
			if (ret_val == NRF_SUCCESS)
			{
				nSumSend += pkgLen;
			}
	        else if ((ret_val == NRF_ERROR_BUSY) || (ret_val == NRF_ERROR_RESOURCES))
				vTaskDelay(1);
            else break;
			if (xTaskGetTickCount()-xLastTime >= configTICK_RATE_HZ*10) break;
	    } while (1);
		NRF_LOG_INFO("speed = %u bytes/sec", nSumSend/10);
		
		sprintf((char *)pszSend, "speed = %u bytes/sec\n", nSumSend/10);
		do
		{
			ret_val = ble_nus_c_string_send(get_nus_c_object(), pszSend, strlen((char*)pszSend));
			if (ret_val == NRF_SUCCESS) break;
			if ((ret_val == NRF_ERROR_BUSY) || (ret_val == NRF_ERROR_RESOURCES))
				vTaskDelay(1);
			else break;
		} while (1);
		
		
		vPortFree(pszSend);
	}
}

static bool tk_main_docommand_here(uint32_t uActID)
{
	bool bRet = false;
	switch (uActID)
	{
	case TK_MAIN_ACT_CONNECTED:
		tk_main_writeSpeedCal();
		bRet = true;
		break;
	case TK_MAIN_ACT_DISCONNECTED:
		bRet = true;
		break;
	}
	return bRet;
}

static void tk_main_prepare()
{
	
}

/**@brief Thread for handling the async command.
 *
 * @details This thread is responsible for do async command.
 *
 * @param[in]   arg   Pointer used for passing some arbitrary information (context) from the
 *                    osThreadCreate() call to the thread.
 */
static void tk_main_thread(void * arg)
{
	uint32_t   ulNotificationValue;
	TickType_t xTicksToWait = portMAX_DELAY;
	
    UNUSED_PARAMETER(arg);

    NRF_LOG_DEBUG("Enter \"tk_Main\" thread.");
	tk_main_prepare();
    while (true)
    {
        ulNotificationValue = ulTaskNotifyTake(pdTRUE,         /* Clear the notification value before exiting (equivalent to the binary semaphore). */
                                                xTicksToWait); /* Block indefinitely (INCLUDE_vTaskSuspend has to be enabled).*/

		NRF_LOG_DEBUG("tk_Main: %u", ulNotificationValue);
		tk_main_docommand_here(ulNotificationValue);
    }
}

//////////////////////////////////////////////////////////////////////////

void tk_main_init(UBaseType_t uxPriority)
{
	if (m_main_thread == (TaskHandle_t)0)
	{
	    if (pdPASS != xTaskCreate(tk_main_thread, "tk_Main", 512, NULL, uxPriority, &m_main_thread))
	    {
	        APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
	    }
	}
}

bool tk_main_setAction(uint32_t iActID)
{
	return xTaskGenericNotify(m_main_thread, iActID, eSetValueWithOverwrite, NULL) == pdPASS;
}

